home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / THREEDOM.ZIP / THREEDOM / TYPEDEF.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.8 KB  |  307 lines

  1. /* @(#)typedef.h 1.15   8/28/96 */
  2.  
  3. /******************************************************************************
  4. * Threedom: a 3D polygon renderer                                             *
  5. * (C) Copyright 1996 by Philip Stephens                                       *
  6. * (C) Copyright 1996 by the IBM Corporation                                   *
  7. * All Rights Reserved                                                         *
  8. *                                                                             *
  9. * Permission to use, copy, modify, and distribute this software and its       *
  10. * documentation without fee for any non-commerical purpose is hereby granted, *
  11. * provided that the above copyright notice appears on all copies and that     *
  12. * both that copyright notice and this permission notice appear in all         *
  13. * supporting documentation.                                                   *
  14. *                                                                             *
  15. * NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY  *
  16. * PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.       *
  17. * NEITHER PHILIP STEPHENS OR IBM SHALL BE LIABLE FOR ANY DAMAGES SUFFERED BY  *
  18. * THE USE OF THIS SOFTWARE.                                                   *
  19. ******************************************************************************/
  20.  
  21. /*
  22.  * Type definitions for various standard integer types.
  23.  */
  24. typedef int boolean;
  25. typedef unsigned char byte;
  26. typedef unsigned short word;
  27. #ifdef    NO_FIXED_MATH
  28. typedef double fixed;
  29. #else
  30. typedef long long fixed;
  31. #endif
  32. typedef unsigned long time_ms;
  33.  
  34. /*
  35.  * Definition of a pixel and frame buffer pixel.
  36.  */
  37. typedef word pixel;
  38. typedef byte fbpixel;
  39.  
  40. /*
  41.  * Pixel colour constants.
  42.  */
  43. #define PIXEL_COLOURS        4096
  44.  
  45. #define PIXEL_REDS        16
  46. #define PIXEL_RED_MASK        0xf000
  47. #define PIXEL_RED_SHIFT        4
  48.  
  49. #define PIXEL_GREENS        16
  50. #define PIXEL_GREEN_MASK    0xf000
  51. #define PIXEL_GREEN_SHIFT    8
  52.  
  53. #define PIXEL_BLUES        16
  54. #define PIXEL_BLUE_MASK        0xf000
  55. #define PIXEL_BLUE_SHIFT    12
  56.  
  57. /*
  58.  * Frame buffer colour constants.
  59.  */
  60. #define FB_COLOURS        192
  61. #define FB_MAPS            3
  62. #define FB_COLOURS_PER_MAP    64
  63. #define FB_MAP_OFFSET        32
  64.  
  65. #define FB_BLACK        FB_MAP_OFFSET
  66. #define FB_WHITE        FB_MAP_OFFSET + 63
  67.  
  68. #ifndef WIN32
  69. #define FB_REDS            4
  70. #define FB_RED_SHIFT        14
  71. #define FB_GREENS        4
  72. #define FB_GREEN_SHIFT        14
  73. #define FB_BLUES        4
  74. #define FB_BLUE_SHIFT        14
  75. #else
  76. #define FB_REDS            4
  77. #define FB_RED_SHIFT        6
  78. #define FB_GREENS        4
  79. #define FB_GREEN_SHIFT        6
  80. #define FB_BLUES        4
  81. #define FB_BLUE_SHIFT        6
  82. #endif
  83.  
  84. /*
  85.  * Lighting constants
  86.  */
  87. #define LIGHT_MAX    5
  88.  
  89. /*
  90.  * Miscellaneous macros and constants.
  91.  */
  92. #define MYMIN(A,B)     ((A) < (B) ? (A) : (B))
  93. #define MYMAX(A,B)     ((A) > (B) ? (A) : (B))
  94.  
  95. /*
  96.  * OS-dependent macros.
  97.  */
  98. #ifdef BSD
  99. #define MEMCOPY(ptr1, ptr2, size)    bcopy(ptr1, ptr2, size)
  100. #define MEMZERO(ptr, size)        bzero(ptr, size)
  101. #else
  102. #define MEMCOPY(ptr1, ptr2, size)    memcpy(ptr2, ptr1, size)
  103. #define MEMZERO(ptr, size)        memset(ptr, 0, size)
  104. #endif
  105.  
  106. /*
  107.  * Boolean type definition and macros.
  108.  */
  109. #ifndef TRUE
  110. #define TRUE    1
  111. #endif
  112. #ifndef FALSE
  113. #define FALSE    0
  114. #endif
  115.  
  116. /*
  117.  * Fixed-point constants.
  118.  */
  119. #define MIN_FIXED    -32768.0
  120. #define MAX_FIXED    32767.0
  121.  
  122. /*
  123.  * Miscellaneous macros.
  124.  */
  125. #define RAD(x)        ((x) * M_PI / 180.0)
  126. #define ABS(x)        ((x) < 0 ? -(x) : (x))
  127.  
  128. /*
  129.  * Flags for find_vertex.
  130.  */
  131. #define FIND_TOP    0
  132. #define FIND_BOTTOM    1
  133.  
  134. /*
  135.  * Motion defines.
  136.  */
  137. #define TURN_RATE    1
  138.  
  139. /*
  140.  * Directory seperator.
  141.  */
  142. #ifndef WIN32
  143. #define DIR_SEPARATOR_CHAR    '/'
  144. #define DIR_SEPARATOR_STR    "/"
  145. #else
  146. #define DIR_SEPARATOR_CHAR    '\\'
  147. #define DIR_SEPARATOR_STR    "\\"
  148. #endif
  149.  
  150. #if defined(__GNUC__) && defined(ARCH_i86)
  151.  
  152. /*
  153.  * Inline assembly functions to perform fixed-point math.
  154.  */
  155. static inline fixed fmul(fixed r1, fixed r2)
  156. {
  157.      fixed result;
  158.  
  159.      __asm__ ("imull %2\n\t"
  160.           "shrd  $16, %%edx, %%eax\n\t"
  161.           :"=a" (result):"a" (r1), "d" (r2):"eax", "edx");
  162.  
  163.      return result;
  164. }
  165.  
  166. static inline fixed fmul2_30(fixed r1, fixed r2)
  167. {
  168.      fixed result;
  169.  
  170.      __asm__ ("imull %2\n\t"
  171.           "shrd  $30, %%edx, %%eax\n\t"
  172.           :"=a" (result):"a" (r1), "d" (r2):"eax", "edx");
  173.  
  174.      return result;
  175. }
  176.  
  177.  
  178. static inline fixed fdiv(fixed dividend, fixed divisor)
  179. {
  180.      fixed result;
  181.  
  182.      /* no checking for overflow is done yet */
  183.      __asm__("movl %%edx, %%eax\n\t"
  184.          "sar  $16, %%edx\n\t"
  185.          "shl  $16, %%eax\n\t"
  186.          "idivl %%ecx, %%eax\n\t"
  187.          :"=a" (result):"d" (dividend), "c" (divisor):"eax", "ecx", "edx");
  188.  
  189.      return result;
  190. }
  191.  
  192. #else
  193.  
  194. /*
  195.  * Macros to perform fixed-point math.
  196.  */
  197. #ifdef    NO_FIXED_MATH
  198.  
  199. #define FIXED_ONE    1.0
  200. #define MOVE_RATE    0.125
  201.  
  202. #define fmul(x,y)    (fixed)((double)(x) * (double)(y))
  203. #define fdiv(x,y)    (fixed)((double)(x) / (double)(y))
  204. #define FIXED(x)    (fixed)(x)
  205. #define INT(x)        (int)(x)
  206. #define INT_TO_FIXED(x)    (fixed)(x)
  207. #define FLOAT(x)    (float)(x)
  208. #define CEIL(x)        (fixed)((int)(x) + 1)
  209.  
  210. #else
  211.  
  212. #define FIXED_ONE    16777216
  213. #define MOVE_RATE    2097152
  214.  
  215. #define FIXED(x)    (fixed)((double)(x) * 16777216.0)
  216. #define FLOAT(x)    ((double)(x) / 16777216.0)
  217. #define fmul(x,y)    (fixed)((double)(x) * (double)(y) / 16777216.0)
  218. #define fdiv(x,y)    (fixed)(((double)(x) * 16777216.0) / (double)(y))
  219. #define INT(x)        (int)((x) >> 24)
  220. #define INT_TO_FIXED(x)    ((fixed)(x) << 24)
  221. #define CEIL(x)        ((((x) + FIXED_ONE) >> 24) << 24)
  222.  
  223. #endif
  224.  
  225. #endif    /* GNU_C && i86 */
  226.  
  227. /*
  228.  * Movement and collision flags.
  229.  */
  230. #define NONE        0
  231.  
  232. #define FORWARD        1
  233. #define BACKWARD    2
  234. #define LEFT        3
  235. #define RIGHT        4
  236.  
  237. #define TURNLEFT    1
  238. #define TURNRIGHT    2
  239. #define LOOKUP        3
  240. #define LOOKDOWN    4
  241.  
  242. /*
  243.  * A generic event record, used for the graphics API.
  244.  */
  245. typedef struct {
  246.     boolean resize;        /* if TRUE, window_width and window_height */
  247.     int window_width;    /* are valid. */
  248.     int window_height;
  249.     int move;    
  250.     int turn;
  251.     boolean fast;
  252.     char *world_file_name;    /* valid if not NULL */
  253. } event_record;
  254.  
  255. /*
  256.  * Data type required for the GIF loader.
  257.  */
  258. typedef struct {
  259.     byte red, green, blue;
  260. } RGBcolor;
  261.  
  262. /*
  263.  * Vertex structure.
  264.  */
  265. typedef struct {
  266.     fixed x, y, z;            /* original vertex */
  267.     fixed tx, ty, tz;        /* transformed vertex */
  268. } vertex;
  269.  
  270. /*
  271.  * Texture point structure.
  272.  */
  273. typedef struct {
  274.     fixed u, v;            /* original texture point */
  275. } tpoint;
  276.  
  277. /*
  278.  * Screen point structure.
  279.  */
  280. typedef struct {
  281.     fixed sx, sy;            /* projected screen point */
  282.     fixed one_on_tz;        /* 1/tz of point */
  283.     fixed u_on_tz, v_on_tz;        /* u/tz and v/tz for texture point */
  284. } spoint;
  285.  
  286. /*
  287.  * Texture map structure.
  288.  */
  289. typedef struct {
  290.     pixel *image_ptr;
  291.     fixed width, height;
  292.     int width_mask, height_mask;
  293.     int column_shift;
  294. } texture;
  295.  
  296. /*
  297.  * Polygon structure.
  298.  */
  299. typedef struct {
  300.     int vertices;            /* Number of edges */
  301.     vertex **vertex_ptr_list;    /* List of edges in clockwise order */
  302.     tpoint **tpoint_ptr_list;    /* List of texture points */
  303.     texture *texture_ptr;        /* Texture to render on polygon */
  304.     vertex normal_vertex;        /* Vertex representing surface normal */
  305.     int light_level;        /* Light level for this polygon */
  306. } polygon;
  307.